
"""
Copyright 2009 Andrew T. Barker

This file is part of Second Budget.

Second Budget is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

Second Budget is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Second Budget, in the file LICENSE.  If not, see 
<http://www.gnu.org/licenses/>.
"""

# time management utility

# written by Andrew T. Barker

# This acts very much like a sinister snooping utility, but
# it's not intended for that.  The idea is it takes screenshots
# at random intervals, at the end of the day I look and see how 
# many of them show me surfing the web.

import random, datetime, time, commands
import sbconfig

# add some logging?  currently only logging is in sbscore, but perhaps we want more

class SecondBudget:
    def __init__(self):
        self.s = sbconfig.SBConfig()
        self.starttime = datetime.datetime.now()

    def screenshotstring(self, a = None):
        if a == None:
            a = datetime.datetime.now()
        stra = a.strftime(self.s.stampfmt)
        filename = self.s.directory + self.s.prefix + stra + "." + self.s.format
        return self.s.invoke + filename

    def run(self):
        j = 0
        while j < self.s.maxshots and datetime.datetime.now() < self.starttime + datetime.timedelta(hours = self.s.maxtimerunning):
            nextinterval = random.randint(self.s.mininterval,self.s.maxinterval)
            time.sleep(nextinterval * 60)
            out = commands.getstatusoutput(self.screenshotstring())
            j = j + 1
            if not out[0] == 0:
                print "Error: screenshot failed."
                print "attempted command:", self.screenshotstring()

def main():
    a = SecondBudget()
    a.run()

if __name__ == "__main__":
    main()
